﻿using NPinyin;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace StringToClass
{
    public partial class Form1: Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //^_^ADD BY FANGHEAO 20210825 FOR 除去字符串中间的空格
            string txt = Regex.Replace(textBox1.Text, @"\s", "");
            textBox2.Text = GetEntityStringToEntity(txt);
        }

        /// <summary>
        /// 字符串转换为实体类字符串
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns>实体类字符串</returns>
        public String GetEntityStringToEntity(string str)
        {
            string[] array = null;
            string[] temp = null;
            string entry = "";
            string type = "";
            string field = "";
            bool sign = true;
            try
            {
                array = str.Split(',');
                foreach (string s in array)
                {
                    type = "";
                    sign = true;
                    temp = s.Split('/');
                    field = temp[0].Replace("\r\n", "");
                    //^_^ADD BY FANGHEAO 20210825 FOR 判断字段中是否有汉字
                    if (HasChinese(field)&& checkBox1.Checked)
                    {
                        field = Transformation(field);
                    }
                    //^_^ADD BY FANGHEAO 20210825 FOR 判断端长度是否为空
                    if (!string.IsNullOrEmpty(temp[2]))
                    {
                        type = $"\r\n[StringLength({temp[2]})]\r\n";
                        sign = false;
                    }
                    //^_^ADD BY FANGHEAO 20210825 FOR 判断端描述是否为空
                    if (!string.IsNullOrEmpty(temp[1]))
                    {
                        if (sign)
                            type = type + $"\r\n[Description(\"{temp[1]}\")]\r\n";
                        else
                            type = type + $"[Description(\"{temp[1]}\")]\r\n";
                        sign = false;
                    }
                    //^_^ADD BY FANGHEAO 20210825 FOR 判断是否允许为空
                    if (temp[3]=="Y")
                    {
                        if (sign)
                            type = type + $"\r\n[Required(ErrorMessage = \"{temp[1]}不能为空！\")]\r\n";
                        else
                            type = type + $"[Required(ErrorMessage = \"{temp[1]}不能为空！\")]\r\n";
                        sign = false;
                    }
                    //^_^ADD BY FANGHEAO 20210825 FOR 长度、描述都为空时开头处换行
                    if (sign)
                        type = type + $"\r\npublic string {field}" + " { get; set; }\r\n";
                    else
                        type = type + $"public string {field}" + " { get; set; }\r\n";
                    entry = entry + type;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return entry;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox3.Text= "type / 类型/6/Y,\r\nname/名称/200/N";
        }

        /// <summary>
        /// 判断字符串中是否包含中文
        /// </summary>
        /// <param name="str">需要判断的字符串</param>
        /// <returns>判断结果</returns>
        public bool HasChinese(string str)
        {
            return Regex.IsMatch(str, @"[\u4e00-\u9fa5]");
        }

        /// <summary>
        /// 汉字字段转拼音
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns>首字母大写</returns>
        public string Transformation(string str)
        {
            string value = "";
            try
            {
                Encoding gb2312 = Encoding.GetEncoding("GB2312");
                string gb = Pinyin.ConvertEncoding(str, Encoding.UTF8, gb2312);
                string py = Pinyin.GetInitials(gb, gb2312).ToLower();
                value = py.First().ToString().ToUpper() + py.Substring(1);
            }
            catch (Exception)
            {
            }
            return value;
        }

    }
}
